/*
 * $Id$
 *
 * Copyright (c) 1998-2008 John Morrison.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */


/*
 * TrackerSetupDialog.java
 *
 * Created on 22 May 2004, 10:40
 */

package com.jbolo.gui.dialog;

import com.jbolo.gui.BoloPreferences;
import com.jbolo.bolo.Utils;

/**
 *
 * @author  John Morrison
 */
public class TrackerSetupDialog extends CentredDialog {
   
    /** Tracker address */
    private String address;
    /** Tracker Port */
    private int port;
    /** Is the tracker in use */
    private boolean useTracker;
    
    /** Creates new form TrackerSetupDialog */
    public TrackerSetupDialog(java.awt.Frame parent, boolean modal) {
        super(parent, "Tracker Config", modal);
        setup();
    }

    public TrackerSetupDialog(java.awt.Dialog parent, boolean modal) {
        super(parent, "Tracker Config", modal);
        setup();
    }
    
    
    private void setup() {
        initComponents();
        setSize(280,250);
        
        txtPort.addKeyListener(new NumberTextFieldListener());
        
        
        BoloPreferences bp = BoloPreferences.GetInstance();
        address = bp.getPrivateProfileString("TRACKER", "Address", "tracker.winbolo.com");
        try {
            port = Integer.parseInt(bp.getPrivateProfileString("TRACKER", "Port", "50000"));
        } catch (Exception e) {
            port = 50000;
        }
        try {
            useTracker = Utils.ConvertYesNoToBool(bp.getPrivateProfileString("TRACKER", "Enabled", "yes"));
        } catch (Exception e) {
            useTracker = false;
        }
        
        txtAddress.setText(address);
        txtPort.setText(Integer.toString(port));
        if (useTracker == true) {
            chkUseTracker.setState(true);
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        java.awt.Button button2;
        java.awt.Label label1;
        java.awt.Label label2;

        label1 = new java.awt.Label();
        label2 = new java.awt.Label();
        txtAddress = new java.awt.TextField();
        txtPort = new java.awt.TextField();
        chkUseTracker = new java.awt.Checkbox();
        button1 = new java.awt.Button();
        button2 = new java.awt.Button();

        setLayout(null);

        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        label1.setAlignment(java.awt.Label.RIGHT);
        label1.setText("Tracker Address: ");
        add(label1);
        label1.setBounds(10, 60, 110, 20);

        label2.setAlignment(java.awt.Label.RIGHT);
        label2.setText("Tracker Port: ");
        add(label2);
        label2.setBounds(30, 100, 90, 20);

        add(txtAddress);
        txtAddress.setBounds(120, 60, 140, 20);

        txtPort.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPortActionPerformed(evt);
            }
        });

        add(txtPort);
        txtPort.setBounds(120, 100, 140, 20);

        chkUseTracker.setLabel("Use Tracker");
        chkUseTracker.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                useTrackerStateChanged(evt);
            }
        });

        add(chkUseTracker);
        chkUseTracker.setBounds(100, 140, 84, 20);

        button1.setLabel("OK");
        button1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button1ActionPerformed(evt);
            }
        });

        add(button1);
        button1.setBounds(60, 190, 57, 24);

        button2.setLabel("Cancel");
        button2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                button2ActionPerformed(evt);
            }
        });

        add(button2);
        button2.setBounds(140, 190, 57, 24);

        pack();
    }//GEN-END:initComponents

    private void button2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button2ActionPerformed
        // TODO add your handling code here:
       closeDialog(null);
    }//GEN-LAST:event_button2ActionPerformed

    private void button1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_button1ActionPerformed
        boolean closeIt = true; // Are we OK to close the window
        BoloPreferences bp = BoloPreferences.GetInstance();

        if (chkUseTracker.getState() == true){
            if (txtAddress.getText().length() == 0) {
                new MessageBox(this, "Address can not be empty if tracker is enabled");
                closeIt = false;
            }
            if (txtPort.getText().length() == 0) {
                new MessageBox(this, "Port can not be empty if tracker is enabled");
                closeIt = false;
            }
            
            if (closeIt == true) {
                bp.writePriveteProfileString("TRACKER", "Enabled", "yes");
                bp.writePriveteProfileString("TRACKER", "Address", txtAddress.getText());
                bp.writePriveteProfileString("TRACKER", "Port", txtPort.getText());
                
            }
            
                        
        } else {
            bp.writePriveteProfileString("TRACKER", "Enabled", "no");
        }
        if (closeIt == true) {
            closeDialog(null);
        }
    }//GEN-LAST:event_button1ActionPerformed

    private void useTrackerStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_useTrackerStateChanged
        // TODO add your handling code here:
        if (evt.getStateChange() == java.awt.event.ItemEvent.SELECTED) {
            txtAddress.setEnabled(true);
            txtPort.setEnabled(true);
        } else {
            txtAddress.setEnabled(false);
            txtPort.setEnabled(false);
        }
    }//GEN-LAST:event_useTrackerStateChanged

    private void txtPortActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPortActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPortActionPerformed
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        setVisible(false);
        dispose();
    }//GEN-LAST:event_closeDialog
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private java.awt.Button button1;
    private java.awt.Checkbox chkUseTracker;
    private java.awt.TextField txtAddress;
    private java.awt.TextField txtPort;
    // End of variables declaration//GEN-END:variables
    
}
